using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;

public abstract class BasePanel : MonoBehaviour
{
    CanvasGroup canvasGroup;

    public float fadeSpeed;

    private UnityAction hideEvent;

    protected bool isShow;

    protected virtual void Awake()
    {

        canvasGroup = GetComponent<CanvasGroup>();
        if (canvasGroup == null)
        { 
            canvasGroup = gameObject.AddComponent<CanvasGroup>();
        }

        Init();
    }


    protected abstract void Init();

    public virtual void ShowMe()
    {
        isShow = true;
        canvasGroup.alpha = 0;
    }

    public virtual void HideMe(UnityAction cb)
    {
        isShow = false;
        canvasGroup.alpha = 1;
        hideEvent = cb;
    }

    protected virtual void Update()
    {
        if (isShow&& canvasGroup.alpha!=1)
        {
            canvasGroup.alpha += fadeSpeed * Time.deltaTime;
            if (canvasGroup.alpha >= 1)
            {
                canvasGroup.alpha = 1;
            }
        }
        else if (!isShow)
        {
            canvasGroup.alpha -= fadeSpeed * Time.deltaTime;
            if (canvasGroup.alpha <= 0)
            {
                canvasGroup.alpha = 0;
                hideEvent?.Invoke();
            }
        }

    }



    
}
